/*
    time.c.tst - Unit tests for Time and Date routines

    Copyright (c) All Rights Reserved. See details at the end of the file.
 */

/********************************** Includes **********************************/

#include    "testme.h"
#include    "mpr.h"

/************************************ Code ************************************/

static void timeBasics()
{
    MprTime     now;
    MprTicks    mark, remaining, elapsed;

    now = mprGetTime();
    ttrue(now != 0);
    
    mark = mprGetTicks();
    ttrue(mark != 0);
    remaining = mprGetRemainingTicks(mark, 30000);
    ttrue(0 <= remaining && remaining <= 30000);

    elapsed = mprGetElapsedTicks(mark);
    ttrue(0 <= elapsed && elapsed < 30000);

    mprSleep(21);
    now = mprGetTime();
    ttrue(mprCompareTime(mark, now) <= 0);
}


static void zones()
{
    MprTime     now;
    struct  tm  tm;

    /* TODO - need some tests here */
    now = mprGetTime();
    mprDecodeLocalTime(&tm, now);
    mprDecodeUniversalTime(&tm, now);
}


static void formatTime()
{
    MprTime     now;
    struct  tm  tm;
    char        *str;

    now = mprGetTime();
    mprDecodeLocalTime(&tm, now);
    str = mprFormatTm(NULL, &tm);
    ttrue(str && *str);
}


static void parseTime()
{
    MprTime     when;

    when = 0;
    ttrue(mprParseTime(&when, "today", MPR_LOCAL_TIMEZONE, NULL) == 0);
    ttrue(mprParseTime(&when, "tomorrow", MPR_LOCAL_TIMEZONE, NULL) == 0);
    ttrue(mprParseTime(&when, "12:00", MPR_LOCAL_TIMEZONE, NULL) == 0);
    ttrue(mprParseTime(&when, "12:30 pm", MPR_LOCAL_TIMEZONE, NULL) == 0);
    ttrue(mprParseTime(&when, "1/31/99", MPR_LOCAL_TIMEZONE, NULL) == 0);
    ttrue(mprParseTime(&when, "Jan 17 2014", MPR_LOCAL_TIMEZONE, NULL) == 0);
    ttrue(mprParseTime(&when, "March 17 2014", MPR_LOCAL_TIMEZONE, NULL) == 0);
    ttrue(when != 0);
}


int main(int argc, char **argv)
{
    mprCreate(argc, argv, 0);

    timeBasics();
    zones();
    formatTime();
    parseTime();

    return 0;
}

/*
    @copy   default

    Copyright (c) Embedthis Software. All Rights Reserved.

    This software is distributed under commercial and open source licenses.
    You may use the Embedthis Open Source license or you may acquire a 
    commercial license from Embedthis Software. You agree to be fully bound
    by the terms of either license. Consult the LICENSE.md distributed with
    this software for full details and other copyrights.

    Local variables:
    tab-width: 4
    c-basic-offset: 4
    End:
    vim: sw=4 ts=4 expandtab

    @end
 */
